/*
 * @(#)GroupRowElement.java 1.0 2005-11-11
 * Copyright 2005 VandaGroup, Inc. All rights reserved.
 */
package com.vanda.report.view;
import java.util.List;

import org.apache.commons.validator.GenericValidator;

import com.vanda.report.ExcelUtils;
import com.vanda.report.ReportException;
import com.vanda.report.ReportGroupRow;
import com.vanda.report.ReportInnerData;
import com.vanda.report.Validation;
import com.vanda.report.element.BeanElement;
/**
 * 类说明:
 * 用于输出汇总行
 * @author wkf3915
 * @version 1.0 2005-11-11
 */
public class GroupRowElement extends BeanElement {
	private int format_row;
	private int start = -1;
	private int end = -1;
	private String start_col;
	private String groupName;

	public void validator() throws ReportException {
		/*Validation.checkRequired(
			"GroupRowElement",
			"format_row",
			format_row,
			0);*/
	}

	/**
	 * 标签执行方法，递归执行
	 * @param obj
	 */
	public void execute(Object obj) throws ReportException {

		try {
			ReportInnerData reportData = (ReportInnerData) obj;
			String property = super.getProperty();

			Object rowData = null;
			Object contextObj = super.getNameObject(reportData);
			//如果为null，则从ContextObject取数据
			if (GenericValidator.isBlankOrNull(property)) {
				rowData = contextObj;
			} else {
				//表示不是数组
				if (start == -1 && end == -1) {
					rowData = super.getProperty(contextObj, property);
				} else {
					if (contextObj instanceof List) {
						if (start >= 0) {
							rowData = ((List) contextObj).get(start);
						} else {
							rowData =
								((List) contextObj).get(
									((List) contextObj).size() - end - 1);
						}
					}

				}
			}
			// 只处理统计行的数据
			if (!(rowData instanceof ReportGroupRow))
				return;
			// 如果指定了group名，只处理本group的数据
			if (!GenericValidator.isBlankOrNull(groupName)) {
				String rowGroupName = ((ReportGroupRow) rowData).getName();
				if (!groupName.equals(rowGroupName))
					return;
			}

			reportData.setContextData(rowData);
			reportData.setFormatRowNum(format_row);
			//如果设置了fomat_row,则将format_row拷贝过来,并删除拷贝行上的数据
			if (format_row > 0) {
				//拷贝format_row,但不拷贝内容
				ExcelUtils.copyRows(
					reportData.getFormatSheet(),
					reportData.getReportSheet(),
					format_row-1,
					format_row-1,
					reportData.getCurrentRow(),
					false,
					false);
			}
			if (start_col != null) {
				int startCol = ExcelUtils.getColIndex(start_col);
				if (startCol > 0) {
					reportData.setStart_col(startCol);
				}
			}
			
			//设置currentCol
			if(reportData.getStart_col() <= 0){
				reportData.setCurrentCol(0);
			}else{
				reportData.setCurrentCol(reportData.getStart_col() - 1);
			}
			
			
			super.execute(obj);

			reportData.setCurrentCol(0);

			//行数＋1
			reportData.setCurrentRow(reportData.getCurrentRow() + 1);

			//System.out.println("row currentRow = "+reportData.getCurrentRow());
		} catch (ReportException e) {
			throw e;
		} catch (Exception e) {
			throw new ReportException(e);
		}

	}
	/**
	 * @return
	 */
	public int getEnd() {
		return end;
	}
	/**
	 * @return
	 */
	public int getFormat_row() {
		return format_row;
	}
	/**
	 * @return
	 */
	public int getStart() {
		return start;
	}
	/**
	 * @param i
	 */
	public void setEnd(int i) {
		end = i;
	}
	/**
	 * @param i
	 */
	public void setFormat_row(int i) {
		format_row = i;
	}
	/**
	 * @param i
	 */
	public void setStart(int i) {
		start = i;
	}
	/**
	 * @return
	 */
	public String getStart_col() {
		return start_col;
	}
	/**
	 * @param string
	 */
	public void setStart_col(String string) {
		start_col = string;
	}
	/**
	 * @return
	 */
	public String getGroupName() {
		return groupName;
	}
	/**
	 * @param string
	 */
	public void setGroupName(String string) {
		groupName = string;
	}
}
